package net.itbaiyang.pattern.email.bridge.mailType;

import net.itbaiyang.pattern.model.EmailDetails;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.io.File;

public class AttachmentsMail implements MailStencil {
    private String to;
    private String subject;
    private String content;

    private String filePath;

    public AttachmentsMail(EmailDetails details) {
        this.to = details.getTo();
        this.subject = details.getSubject();
        this.content = details.getContent();
        this.filePath = details.getFilePath();
    }

    @Override
    public void apply(MimeMessageHelper helper) throws Exception {
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = file.getFilename();
        helper.addAttachment(fileName, file); //设置附件
    }
}
